#import "AddressAnnotation.h"

/**
 * Encapsulates annotation information for the MKMapView. 
 * 
 * Stores the annotation title, subtitle and coordinate
 * @author      William LaFrance (used with permission: http://wjlafrance.net/?p=7)
 * @author      James Lin
 * @version     1.4.3
 * @since       0.2.0
 */
@implementation AddressAnnotation

@synthesize coordinate = annCoordinate;
@synthesize annType;

// Retrieves map annotation with a coordinate parameter
+ (AddressAnnotation*)mapAnnotationWithCoordinate:(CLLocationCoordinate2D)coordinate {
	return [[self alloc] initWithCoordinate:coordinate];
}

// Retrieves map annotation with a coordinate and title
+ (AddressAnnotation*)mapAnnotationWithCoordinate:(CLLocationCoordinate2D)coordinate 
										 andTitle:(NSString*)title {
	return [[self alloc] initWithCoordinate:coordinate andTitle:title];
}

// Retrieves a map annotation with a coordinate, title and subtitle
+ (AddressAnnotation*)mapAnnotationWithCoordinate:(CLLocationCoordinate2D)coordinate 
										 andTitle:(NSString*)title 
									  andSubtitle:(NSString*)subtitle {
	return [[self alloc] initWithCoordinate:coordinate andTitle:title andSubtitle:subtitle];
}

+ (AddressAnnotation*)mapAnnotationWithCoordinate:(CLLocationCoordinate2D)coordinate 
										 andTitle:(NSString*)title 
									  andSubtitle:(NSString*)subtitle 
										  andType:(AnnotationType)type {
	return [[self alloc] initWithCoordinate:coordinate 
								   andTitle:title 
								andSubtitle:subtitle andType:type];
}
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate {
    annCoordinate = coordinate;
    return self;
}

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate 
				andTitle:(NSString*)title {
    annCoordinate = coordinate;
    annTitle = [title retain];
	
    return self;
}

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate 
				andTitle:(NSString*)title 
			 andSubtitle:(NSString*)subtitle {
    annCoordinate = coordinate;
    annTitle = [title retain];
    annSubtitle = [subtitle retain];
	
    return self;
}

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate 
				andTitle:(NSString*)title 
			 andSubtitle:(NSString*)subtitle 
				 andType:(AnnotationType)type{
	if (self = [super init]) {
		annCoordinate = coordinate;
		annTitle = [title retain];
		annSubtitle = [subtitle retain];
		self.annType = type;
	}
	
    return self;
}

- (id)initWithType:(AnnotationType)type {
	if (self = [super init]) {
		self.annType = type;
	}
	
    return self;
}

- (id)initWithCoder:(NSCoder*)decoder {
	if ((self = [self init]) && ([decoder allowsKeyedCoding])) {	
		annTitle = [[decoder decodeObjectForKey:@"AnnotationTitle"] retain];
		annSubtitle = [[decoder decodeObjectForKey:@"AnnotationSubtitle"] retain];
		annCoordinate.latitude = [decoder decodeDoubleForKey:@"AnnotationLatitude"];
		annCoordinate.longitude = [decoder decodeDoubleForKey:@"AnnotationLongitude"];
		self.annType = [decoder decodeIntForKey:@"AnnotationPinType"];
	}
	
	return self;
}

- (void)encodeWithCoder:(NSCoder*)encoder {
	if ([encoder allowsKeyedCoding]) {
		[encoder encodeObject:annTitle forKey:@"AnnotationTitle"];
		[encoder encodeObject:annSubtitle forKey:@"AnnotationSubtitle"];
		[encoder encodeDouble:annCoordinate.latitude forKey:@"AnnotationLatitude"];
		[encoder encodeDouble:annCoordinate.longitude forKey:@"AnnotationLongitude"];
		[encoder encodeInt:self.annType forKey:@"AnnotationPinType"];
	}
}

- (NSString*)getKey {
	return [NSString stringWithFormat:@"%s_%f_%f",self.title, 
			annCoordinate.latitude, 
			annCoordinate.longitude];
}

- (NSString*)title {
    return annTitle;
}

- (NSString*)subtitle {
    return annSubtitle;
}

- (void)setTitle:(NSString*)title {
    annTitle = [title retain];
}

- (void)setSubtitle:(NSString*)subtitle {
    annSubtitle = [subtitle retain];
}

- (void)setCoordinate:(CLLocationCoordinate2D)coordinate {
	annCoordinate = coordinate;
}

- (void)dealloc {
    [annTitle release];
    [annSubtitle release];
    [super dealloc];
}

@end